<?php

class PracticeController extends \Phalcon\Mvc\Controller {

    public function indexAction() {
        
    }

    public function doAction($part, $level) {
        $parameters = array(
            "part = '$part' AND level = '$level'",
            'order' => 'RAND()',
        );
        $questions = array();
        $question = Questions::findFirst($parameters);
        $audio = Audios::findFirstById($question->audio);
        $image = Images::findFirstById($question->img);
        $document = Documents::findFirstById($question->document);
        if ($part == 3 || $part == 4) {
            $qtList = Questions::find("part = '$part' AND level = '$level' AND audio = '$question->audio'");
            $questions = $qtList->toArray();
        } else if ($part == 6 || $part == 7) {
            $qtList = Questions::find("part = '$part' AND level = '$level' AND document = '$question->document'");
            $questions = $qtList->toArray();
        } else {
            array_push($questions, $question->toArray());
        }

        $qts = array();
        foreach ($questions as $value) {
            $qts[] = $value['id'];
        }

        $this->view->qtsStr = implode('_', $qts);
        $this->view->audio = $audio;
        $this->view->image = $image;
        $this->view->document = $document;
        $this->view->questions = $questions;
    }

    public function getAnswerAction() {
        $this->view->disable();
        $document = $this->request->getPost('document');
        $qts = $this->request->getPost('questions');
        $questions = Questions::find("id IN ($qts)");
        $doc = Documents::findFirstById($document);
        $answer = array();
        foreach ($questions as $question) {
            $answer[$question->id] = $question->correct;
        }
        $result = array(
            'doc' => $doc,
            'answer' => $answer,
        );
        $this->response->setStatusCode(200, "OK");
        $this->response->setJsonContent($result);
        $this->response->send();
    }

}
